package telcoblocks.gen;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.wizards.datatransfer.FileSystemExportWizard;
import org.germinus.telcoblocks.configuration.*;


public class ExportConfigurationWizard extends FileSystemExportWizard implements IExportWizard {
	
	private ExportConfigurationWizardPage page;
	
	public ExportConfigurationWizard() {
	}

	@Override
	public final boolean performFinish() {
		if (!page.isDestinationValid((new File(page.getSelectedDirectory())))) {
			return false;
			}
		for (IResource res : page.getSelectedResources()) {
			try {
				File ficheroOrigen = res.getRawLocation().toFile();
				File ficheroDestino = new File(page.getSelectedDirectory(),"sip.conf");
				AsteriskConfiguration a = new AsteriskConfiguration();
				a.generateFile(ficheroOrigen, ficheroDestino);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}
	
	@Override
	public final boolean canFinish() {
		try{
			if((page.getSelectedResources().size() == 1) & !page.getSelectedDirectory().equals("")){			
				if(page.getSelectedResources().get(0).getFileExtension().equals("infraestructuras_diagram")){
					return true;
				}
			}
			return false;
		}
		catch (Exception e1){
			return false;
		}
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.page = new ExportConfigurationWizardPage("ExportPage", selection); //$NON-NLS-1$
		
		super.init(workbench, selection);

	}


	@Override
	 public final void addPages() {
	  addPage(this.page);
	}
}

